<script setup lang="tsx">
import { ref, inject } from 'vue';
import MyTable from '@/components/myTable/index.vue';
import OperatingBox from '@/components/operatingBox/index.vue';
import QueryBox from '@/components/queryBox/index.vue';
import { formGroup } from '@/components/formDialog/FormDialog';
import FormDialogGroup from '@/components/formDialog/FormDialogGroup.vue';
import { Space, Link, MessagePlugin } from 'tdesign-vue-next';
import { AddIcon, SearchIcon } from 'tdesign-icons-vue-next';
import { getList, add, edit, getInfo } from '@/api/lucky/draws';
import { getPrizeList } from '@/api/lucky/prizeAdmin';
import { getName } from '@/utils/tool';
const addDateRange = inject<typeof addDateRange>('addDateRange');
const data = ref([]);
const columns = [
  {
    title: 'ID',
    colKey: 'id',
  },
  {
    title: '用户名',
    colKey: 'username',
  },
  {
    title: '抽奖次数',
    colKey: 'times',
  },
  {
    title: '下次必中',
    colKey: 'prize',
  },
  {
    title: '操作',
    colKey: 'op',
  },
];

const pagination = ref({
  defaultCurrent: 1,
  defaultPageSize: 10,
  total: 0,
});

interface FormDataType {
  username: string | null;
  pageNum: string | number | null;
  pageSize: string | number | null;
}
const INITIAL_QUERYFORM: FormDataType = {
  username: undefined,
  pageNum: null,
  pageSize: null,
};
const queryForm = ref({ ...INITIAL_QUERYFORM });

const dateRange = ref<string[]>([]);
const balanceRange = ref<[number | null, number | null]>([null, null]);
const render = () => (
  <>
    <OperatingBox>
      <t-button icon={() => <AddIcon />} onClick={() => unAdd()}>
        新增
      </t-button>
    </OperatingBox>
    <QueryBox onReset={onReset} v-model:defaultCurrent={pagination.value.defaultCurrent} onSearch={unGetList}>
      <t-input v-model={queryForm.value.username} placeholder="用户名" style="width: 240px;">
        {{
          prefixIcon: () => <SearchIcon />,
        }}
      </t-input>
    </QueryBox>
    <MyTable
      columns={columns}
      data={data.value}
      v-model:pagination={pagination.value}
      rowKey="id"
      onUnGetList={unGetList}
    >
      {{
        prize: (slotProps: any) => <div>{getName('id', slotProps.row.prize, prizeList.value, 'prizeName')}</div>,
        op: (slotProps: any) => (
          <Space>
            <Link theme="primary" onClick={() => unEdit(slotProps)}>
              修改
            </Link>
          </Space>
        ),
      }}
    </MyTable>
  </>
);

const unGetList = async () => {
  try {
    queryForm.value.pageNum = pagination.value.defaultCurrent;
    queryForm.value.pageSize = pagination.value.defaultPageSize;
    const res = await getList({
      ...addDateRange?.(queryForm.value, dateRange.value),
    });
    data.value = res.rows;
    pagination.value = {
      ...pagination.value,
      total: res.total,
    };
  } catch (e) {
    console.log(e);
  }
};

// 获取奖品列表
const prizeList = ref([]);
const unPrizeList = async () => {
  try {
    const res = await getPrizeList({
      pageNum: 1,
      pageSize: 999,
    });
    prizeList.value = res.rows;
    unGetList();
  } catch (e) {
    console.log(e);
  }
};
unPrizeList();

const onReset = () => {
  queryForm.value = { ...INITIAL_QUERYFORM };
  balanceRange.value = [null, null];
  dateRange.value = [];
  unGetList();
};
/** 添加对话框 */
const formObj = new formGroup();
const unAdd = async () => {
  try {
    formObj.show('添加次数', {}, '添加次数');
  } catch (e) {
    console.error(e);
    MessagePlugin.error(e);
  }
};

formObj.add<any>('添加次数', '添加次数', {}, async (body) => {
  await add(body.data);
  unGetList();
});

// 详情
const unEdit = async (slotProps: any) => {
  try {
    let res = await getInfo(slotProps.row.id);
    formObj.show(
      '编辑次数',
      {
        ...res.data,
      },
      '编辑次数',
    );
  } catch (e) {
    console.error(e);
    MessagePlugin.error(e);
  }
};

formObj.add<any>('编辑次数', '编辑次数', {}, async (body) => {
  await edit(body.data);
  unGetList();
});
</script>

<template>
  <div>
    <render />
    <FormDialogGroup :form-obj="formObj">
      <template #default="{ formData, item }">
        <!-- 添加次数对话框 -->
        <template v-if="item == '添加次数' || item == '编辑次数'">
          <t-form-item label="用户名" name="username">
            <t-input v-model="formData.username" placeholder="请输入用户名" />
          </t-form-item>
          <t-form-item label="抽奖次数" name="times">
            <div class="capital">
              <t-input-number v-model="formData.times" placeholder="请输入抽奖次数" />
            </div>
          </t-form-item>
          <t-form-item label="下次必中" name="prize">
            <t-select v-model="formData.prize" placeholder="请选择">
              <t-option v-for="item in prizeList" :key="item.id" :label="item.prizeName" :value="item.id" />
            </t-select>
          </t-form-item>
        </template>
      </template>
    </FormDialogGroup>
  </div>
</template>

<style lang="less" scoped>
.capital .t-input-number.t-input-number--row {
  width: 314px;
  display: flex;
  align-items: center;
  padding: 0 38px;

  .t-button.t-button--variant-outline.t-button--theme-default.t-button--shape-square.t-button--icon-only {
    width: 38px;
    height: 38px;
  }
}
</style>

<style>
.capital .t-input-number .t-input.t-align-center {
  width: 230px !important;
}
</style>
